<template>
	<div>
		<el-form ref="form" :model="form" label-width="80px" size="mini" :rules="rules">
			<el-form-item label="用户名">
				<el-input v-model="form.username"></el-input>
			</el-form-item>
			<el-form-item label="真实姓名">
				<el-input v-model="form.relaname"></el-input>
			</el-form-item>
			<el-form-item label="密码">
				<el-input type="password" v-model="form.password" show-password></el-input>
			</el-form-item>
			<el-form-item label="邮箱" prop="email">
				<el-input v-model="form.email"></el-input>
			</el-form-item>
			<el-form-item label="电话号" prop="telephone">
				<el-input v-model="form.telephone"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" plain @click="save">保存数据</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>
<script>
	import axios from '../../utils/axiosutils.js'
	export default {
		name: 'RoleEdit',
		props: ['editid'],
		data() {
			return {
				form: {
					id: '',
					nickname: '',
					password: '',
					username: '',
					email: '',
					telephone: ''
				},
				rules: {
					telephone: [{
						required: true,
						message: "请输入手机号",
						trigger: "blur"
					}, {
						pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/,
						message: "请输入合法手机号/电话号",
						trigger: "blur"
					}],
					email: [{
						required: true,
						message: "请输入邮箱",
						trigger: "blur"
					}, {
						pattern: /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g,
						message: "请输入正确的邮箱",
						trigger: "blur"
					}],
				}
			}
		},
		created() {
			if (this.editid) {
				axios.get('/user/getById', response => {
					this.form.id = response.obj.id;
					this.form.username = response.obj.username;
					this.form.relaname = response.obj.relaname;
					this.form.email = response.obj.email;
					this.form.telephone = response.obj.telephone;
					this.form.password = response.obj.password;
				}, {
					id: this.editid
				});
			}
		},
		methods: {
			save: function() {
				this.$refs.form.validate(valid => {
					if (valid) {
						axios.post('/user/add', resp => {
							this.$emit('update:show', false);
							this.$emit('getData');
						}, this.form);
					} else {
						console.log("error submit!!");
						return false;
					}
				});
				
			}
		}
	}
</script>
<style>
</style>
